
//
//  lookImageVC.m
//  lyxluyou
//
//  Created by mmyy on 15/10/18.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "lookImageVC.h"
#import "moreImageItems.h"
#import "CollectImage.h"
#import "CoreData+MagicalRecord.h"
#import "LogVC.h"
#import "AFAppDotNetAPIClient.h"
@interface lookImageVC ()<UIScrollViewDelegate, UIAlertViewDelegate, UIActionSheetDelegate>

@end

@implementation lookImageVC
{
    UIScrollView *_scrollView;
    UIView *_toolView;
    NSInteger currentPrice;

}

-(instancetype)init
{
    if (self = [super init]) {
        self.dataSource = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = YES;
    [self createScrollView];
    [self createTool];
    self.navigationItem.title = self.titleName;
    // Do any additional setup after loading the view.
}
-(void)createTool
{
    _toolView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 40)];
    _toolView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_toolView];
    _toolView.hidden = YES;
    UIButton *leftBt = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 2.0, 40)];
    leftBt.backgroundColor = [UIColor clearColor];
    [leftBt setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [leftBt setTitle:@"点击分享" forState:UIControlStateNormal];
    leftBt.layer.borderColor = [UIColor greenColor].CGColor;
    leftBt.layer.borderWidth = 1;
    [_toolView addSubview:leftBt];
    UIButton *rightBt = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2.0, 0, self.view.frame.size.width / 2.0, 40)];
    rightBt.backgroundColor = [UIColor clearColor];
    [rightBt setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [rightBt setTitle:@"点击收藏" forState:UIControlStateNormal];
    rightBt.layer.borderColor = [UIColor greenColor].CGColor;
    rightBt.layer.borderWidth = 1;
    [_toolView addSubview:rightBt];
    
    [leftBt addTarget:self action:@selector(btChange:) forControlEvents:UIControlEventTouchUpInside];
    [rightBt addTarget:self action:@selector(btChange:) forControlEvents:UIControlEventTouchUpInside];
    

}
-(void)btChange:(UIButton *)sender
{
 
    if (![[NSUserDefaults standardUserDefaults] objectForKey:SCREEN_NAME]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未登录" delegate:self cancelButtonTitle:@"现在登录" otherButtonTitles:@"取消", nil];
        [alert show];
        return;
    }
    if ([sender.currentTitle isEqualToString:@"点击收藏"]) {
        currentPrice = _scrollView.contentOffset.x / self.view.frame.size.width;
        if (![CollectImage searchImage:[NSString stringWithFormat:@"%@",[[_dataSource objectAtIndex:currentPrice] trip_id]]]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"图片已收藏" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            return;
        }
        UIImageView *imageV = (UIImageView *)[_scrollView viewWithTag:currentPrice + 10];
        NSData *data;
        if (UIImagePNGRepresentation(imageV.image) == nil) {
            
            data = UIImageJPEGRepresentation(imageV.image, 1);
            
        } else {
            
            data = UIImagePNGRepresentation(imageV.image);
        }
        if (![data isKindOfClass:[NSNull class]]) {
            CollectImage *collect = [CollectImage MR_createEntity];
            collect.vid = [NSString stringWithFormat:@"%@",[[_dataSource objectAtIndex:currentPrice] trip_id]];
            collect.data = data;
            [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"收藏成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"收藏失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        
        }
        
    }else {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"分享微博" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [sheet showInView:_scrollView];
    }
}
-(void)createScrollView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height - 65)];
    _scrollView.backgroundColor = [UIColor blackColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    CGFloat height = (_scrollView.frame.size.height - 300) / 2;
    for (int i = 0; i < self.dataSource.count; i++) {
        UIImageView *imageV = [UIImageView new];
        moreImageItems *models = [self.dataSource objectAtIndex:i];
        [imageV setImageWithURL:[NSURL URLWithString:models.pohot]];
        [_scrollView addSubview:imageV];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_scrollView.top).offset(height);
            make.left.equalTo(_scrollView.left).offset(i * _scrollView.frame.size.width);
            make.width.equalTo(_scrollView.frame.size.width);
            make.height.equalTo(300);
            if (i == self.dataSource.count - 1) {
                make.right.equalTo(_scrollView.right).offset(0);
            }
        }];
        imageV.tag = 10 + i;
        imageV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapChange:)];
        [imageV addGestureRecognizer:tap];
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = models.trip_name;
        titleLabel.textColor = [UIColor redColor];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [_scrollView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_scrollView.top).offset(10);
            make.left.equalTo(_scrollView.left).offset(i * _scrollView.frame.size.width);
            make.width.equalTo(_scrollView.frame.size.width);
            make.height.equalTo(@30);
        }];
    }
    [_scrollView setContentOffset:CGPointMake(self.currentPice * self.view.frame.size.width, 0)];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 10000) {
        if (buttonIndex == 0) {
            UIImageView *imageV = (UIImageView *)[_scrollView viewWithTag:currentPrice + 10];
                [Funcation sendWeiboWithBlock:^(NSArray *posts, NSError *error) {
                    NSLog(@"Success");
                } andContent:[[alertView textFieldAtIndex:0] text] andImage:imageV.image];
        }
    }else {
    if (buttonIndex == 0) {
        LogVC *vc = [[LogVC alloc] init];
        UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:nvc animated:YES completion:nil];
    }
    }
}
-(void)tapChange:(UITapGestureRecognizer *)sender
{
    _toolView.hidden = NO;
//    [self.view bringSubviewToFront:_toolView];

}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _toolView.hidden = YES;
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"请输入分享文字" message:nil delegate:self cancelButtonTitle:@"发送" otherButtonTitles:@"取消", nil];
        alter.alertViewStyle = UIAlertViewStylePlainTextInput;
        alter.tag = 10000;
        [alter show];

    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
